﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetDevices.aspx.cs" Inherits="GetDevices" %>


<input type="checkbox" id="select_all_devices" />
<label for="select_all_devices">All Devices</label>
<div class="wrapper_check">
    <ul id="devices_list">
        <%= this.GetUIData() %>       
    </ul>
</div>
<script>

    $(document).ready(function () {
        function listFilter(header, list) {
            input = $("<input>").attr({ "id": "filter_devices_text", "class": "filterinput inputnormalwide", "type": "text", "placeholder": "Type to begin filtering..." });
            clear = $("<span>").text(" clear").attr({ "id": "clear_devices_filter", "style": "cursor:pointer" });

            $(input).insertBefore(header);
            $(clear).insertBefore(header);
            $("<br/>").insertBefore(header);


            $(clear).hide();

            $(input).change(function () {

                if ($(input).val().length > 0) {
                    $(clear).show();
                }
                else {
                    $(clear).hide();
                }

                var filter = $(this).val().toLowerCase();
                if (filter) {
                    $(list).find("label").each(function () {
                        if ($(this).text().toLowerCase().indexOf(filter) === -1 && !($(this).parent().hasClass("deleted"))) {
                            $(this).parent().slideUp();
                        } else if ($(this).text().toLowerCase().indexOf(filter) !== -1 && !($(this).parent().hasClass("deleted"))) {
                            $(this).parent().slideDown();
                        }
                    });
                } else {
                    $(list).find("label:not(:empty)").parent().slideDown();
                    if ($("#devices_list>li>input:[id^='inputee']:not(:checked)").length > 0) {
                        $("#select_all_devices").attr('checked', false);
                    } else {
                        $("#select_all_devices").attr('checked', true);
                    }
                }
                return false;
            })
            .keyup(function () {
                $(this).change();
            });
        }
        listFilter($("#select_all_devices"), $("#devices_list"));
        $("#clear_devices_filter").click(function (event) {
            $('#filter_devices_text').val('');
            $('#filter_devices_text').change();
        });

    });
    $("#select_all_devices").live("change", function (event) {

        // This was added because the 'All type' checkbox wasn't having its checked attribute removed properly
        $(this).attr("checked", $(this).attr("checked") ? true : false);

        $($("#devices_list>li>input:[id^='inputee']").filter(':visible')).attr('checked', $(this).is(':checked')).next().css('font-weight', $(this).is(':checked') ? 'bold' : 'normal');
    });

    $("#select_all_devices").attr('checked', true);

    $('input:[id^="inputee"]').live("change", function (event) {
        var unchecked_present = true;
        $('input:[id^="inputee"]').each(function (i) {

            if (!$(this).is(':checked')) {
                unchecked_present = false;
            }

        });
        $("#select_all_devices").attr('checked', unchecked_present);
        $(this).next().css('font-weight', $(this).is(':checked') ? 'bold' : 'normal');
    });





</script>
